Python Geospatial Data Analysis
Import Libraries¶
In [27]:
# Standard library imports
import math
# Third-party library imports
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import folium
from folium.plugins import MarkerCluster, HeatMap
Import Data¶
In [5]:
# Load the data from the provided URL into a pandas DataFrame
df = pd.read_csv('https://data.boston.gov/dataset/6220d948-eae2-4e4b-8723-2dc8e67722a3/resource/e86f8e38-a23c-4c1a-8455-c8f94210a8f1/download/tmpf_uzkqpk.csv')
# Display the first 6 rows of the DataFrame to get a quick overview of the data
df.head(6)
Out[5]:
| INCIDENT_NUMBER | OFFENSE_CODE | OFFENSE_CODE_GROUP | OFFENSE_DESCRIPTION | DISTRICT | REPORTING_AREA | SHOOTING | OCCURRED_ON_DATE | YEAR | MONTH | DAY_OF_WEEK | HOUR | UCR_PART | STREET | Lat | Long | Location | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | I192074715 | 2629 | Harassment | HARASSMENT | B2 | 278 | NaN | 2018-01-01 00:00:00 | 2018 | 1 | Monday | 0 | Part Two | HARRISON AVE | 42.331538 | -71.080157 | (42.33153805, -71.08015661) |
| 1 | I192068538 | 1107 | Fraud | FRAUD - IMPERSONATION | D14 | 794 | NaN | 2018-01-01 00:00:00 | 2018 | 1 | Monday | 0 | Part Two | GLENVILLE AVE | 42.349780 | -71.134230 | (42.34977988, -71.13423049) |
| 2 | I192005657 | 2610 | Other | TRESPASSING | C11 | 396 | NaN | 2018-01-01 00:00:00 | 2018 | 1 | Monday | 0 | Part Two | MELBOURNE ST | 42.291093 | -71.065945 | (42.29109287, -71.06594539) |
| 3 | I192075335 | 3208 | Property Lost | PROPERTY - MISSING | D4 | 132 | NaN | 2018-01-01 00:00:00 | 2018 | 1 | Monday | 0 | Part Three | COMMONWEALTH AVE | 42.353522 | -71.072838 | (42.35352153, -71.07283786) |
| 4 | I192013179 | 619 | Larceny | LARCENY ALL OTHERS | C11 | 360 | NaN | 2018-01-01 00:00:00 | 2018 | 1 | Monday | 0 | Part One | CENTERVALE PARK | 42.296323 | -71.063569 | (42.29632282, -71.06356881) |
| 5 | I182072846 | 617 | Larceny | LARCENY THEFT FROM BUILDING | C11 | 353 | NaN | 2018-01-01 00:00:00 | 2018 | 1 | Monday | 0 | Part One | FREEPORT ST | 42.301499 | -71.050712 | (42.30149875, -71.05071215) |
Generate Base Map¶
A BASE MAP OF BOSTON AREA
In [6]:
# Define the latitude and longitude coordinates for Boston
boston = (42.358443, -71.05977)
# Create a folium map centered around Boston
m = folium.Map(location=boston, title='Stamen terrain', zoom_start=12)
# Display the map
m
Out[6]:
Make this Notebook Trusted to load map: File -> Trust Notebook
Mark Crime Scenes¶
A MAP SHOWING DISTRICTS WITH THE HIGHEST CRIME RATES
In [7]:
# Create a MarkerCluster to group markers together for efficient map rendering
mc = MarkerCluster()
# Iterate through the rows of the DataFrame 'df'
# 'idx' is the index of the row, and 'row' is the data in the row as a Pandas Series
for idx, row in df.iterrows():
# Check if the 'Long' and 'Lat' values in the current row are not NaN (not missing)
if not math.isnan(row['Long']) and not math.isnan(row['Lat']):
# If the 'Long' and 'Lat' values are valid, add a folium.Marker to the MarkerCluster
# with the latitude and longitude extracted from the current row
mc.add_child(folium.Marker([row['Lat'], row['Long']]))
# Add the MarkerCluster 'mc' to the folium map 'm'
m.add_child(mc)
Out[7]:
Make this Notebook Trusted to load map: File -> Trust Notebook